package opera1;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        Scanner input = new Scanner(System.in);
        int currentUsedSize = bookList.getUsedSize();
        System.out.println("请输入你要删除的书名: ");
        String name = input.nextLine();
        for (int i = 0; i < currentUsedSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
            /*

                for (int j = i + 1; j < currentUsedSize; j++) {
                    Book temp = bookList.getBook(j);
                    bookList.setBook(j - 1,temp);
                }*/
                for (int j = i; j < currentUsedSize - 1; j++) {
                    Book temp = bookList.getBook(j + 1);
                    bookList.setBook(j,temp);
                }  // bookList.setBook(currentUsedSize - 1,null);  会给最后一个元素赋值为null
                System.out.println("删除成功!");
                break;
            }
        }
        bookList.setUsedSize(currentUsedSize - 1);
        bookList.setBook(currentUsedSize - 1,null);
    }
}
